package com.fmsz.xmly.widget;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.test.mock.MockApplication;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.fmsz.xmly.BR;
import com.fmsz.xmly.R;
import com.fmsz.xmly.adapters.CommentAdapter;
import com.fmsz.xmly.application.MyApplication;
import com.fmsz.xmly.databinding.ProgramListPopupBinding;
import com.fmsz.xmly.entitys.details.DetailListEntity;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/2/11.
 * 音乐播放页面节目列表下载
 */

public class ProgramListPop implements Callback<DetailListEntity>, View.OnClickListener, PopupWindow.OnDismissListener {

    private final ProgramListPopupBinding binding;
    private final View root;
    private final Activity context;
    private final long albumId;
    private final Handler handler;
    private int displayheight;
    private PopupWindow window;
    private CommentAdapter<Object> adapter;
    private DetailListEntity data;

    public ProgramListPop(Activity context, long albumId, Handler handler) {
        this.context = context;
        this.albumId = albumId;
        this.handler = handler;
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.program_list_popup, null, false);
        root = binding.getRoot();
        initView();
    }

    public void setData(DetailListEntity data) {
        this.data = data;
        adapter.addAll(data.getData().getTracks().getList());
    }

    private void initView() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        displayheight = displayMetrics.heightPixels;//高度
        window = new PopupWindow(root, ViewGroup.LayoutParams.MATCH_PARENT, ((int) (displayheight * 0.70)), true);
        window.setTouchable(true);
        window.setOutsideTouchable(true);
        window.setAnimationStyle(R.style.program_list_anima);
        window.setOnDismissListener(this);
        window.setBackgroundDrawable(new ColorDrawable());
//        ((MyApplication) context.getApplication()).getService().getDetailList(String.valueOf(albumId)).enqueue(this);
        adapter = new CommentAdapter<>(new ArrayList<>(), context, R.layout.program_item, BR.programListData);
        binding.listProgram.setAdapter(adapter);
        binding.offBtn.setOnClickListener(this);
    }

    public void show() {
        Window windows = context.getWindow();
        WindowManager.LayoutParams lp = windows.getAttributes();
        lp.alpha = 0.5f; //0.0-1.0
        context.getWindow().setAttributes(lp);
        this.window.showAtLocation(root, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void onResponse(Call<DetailListEntity> call, Response<DetailListEntity> response) {
        DetailListEntity body = response.body();
        if (body != null) {
            adapter.addAll(body.getData().getTracks().getList());
        }
    }

    @Override
    public void onFailure(Call<DetailListEntity> call, Throwable t) {
        t.printStackTrace();
        Log.i("1620", "onFailure: " + t.getMessage());
        Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.off_btn:
                window.dismiss();
                break;
        }
    }

    @Override
    public void onDismiss() {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = 1.0f; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }

}
